<?php

/**
 * adminpriceformats actions.
 *
 * @package    retailbrain
 * @subpackage adminpriceformats
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminpriceformatsActions extends sfActions {

    /**
     * Executes priceformats action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Formato Precio');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Formato Precio", "adminpriceformats/index"));
        
        $q = Doctrine_Query::create()
                ->from('PriceFormats pf')
                ->orderBy('pf.name desc');
        
        $this->priceFormats = $q->fetchArray();
     
        $this->caps = $this->getUser()->getAttribute('caps');
    }

    
    /**
     * Executes editPriceFormats action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditPriceFormats(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Formato Precio');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Formato Precio", "adminpriceformats/index"), array("Editar Formato Precio", "adminpriceformats/editPriceFormats?id=$id"));
        
        
        /** Locationes **/
        $q = Doctrine_Query::create()
                ->from('PriceFormats pf')
                ->where('pf.id_priceformat = ?', array($id))
                ->fetchArray();

        $this->priceFormat = $q;
        
    }
    
    
    /**
     * Executes addPriceFormats action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddPriceFormats(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Formato Precio');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Formato Precio", "adminpriceformats/index"), array("Agregar Formato Precio", "adminpriceformats/addPriceFormats"));
        
    }
    
    
    /**
     * Executes createPriceFormat action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreatePriceFormat(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('priceFormats');
        
        try {

            $tabla = Doctrine::getTable('PriceFormats');
            $tabla = new PriceFormats();

            $tabla->name        = strtoupper($_arrayPost['name']);
            
            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/adminpriceformats/index');
    }
    
    
    /**
     * Executes updatePriceFormat action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdatePriceFormat(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('priceFormats');
        
        try {

            $tabla = Doctrine::getTable('PriceFormats')->find(array($_arrayPost["id"]));

            $tabla->name        = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/adminpriceformats/index');
    }
    
    
     /**
     * CustomerActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActivePriceFormat(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('PriceFormats pf')
                ->set('pf.active', '?', $active)
                ->where('pf.id_priceformat = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('PriceFormats pf')
                ->orderBy('pf.name desc');
        
        $this->priceFormats = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstPriceFormats');
    }
    
    /**
     * CustomerDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeletePriceFormat(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('PriceFormats pf')
                ->set('pf.deleted', '?', $deleted)
                ->where('pf.id_priceformat = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('PriceFormats pf')
                ->orderBy('pf.name desc');
        
        $this->priceFormats = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstPriceFormats');
    }
    
}
